/* Turska Framework/UI library (Turs2)
 * Copyright 2003-2010 Jetro Lauha
 * All rights reserved.
 * Web: http://iki.fi/jetro/turska/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of EITHER:
 *   (1) The GNU Lesser General Public License as published by the Free
 *       Software Foundation; either version 2.1 of the License, or (at
 *       your option) any later version. The text of the GNU Lesser
 *       General Public License is included with this library in the
 *       file LICENSE-LGPL.txt.
 *   (2) The BSD-style license that is included with this library in
 *       the file LICENSE-BSD.txt.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
 * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
 *
 * $Id: T2Types.h 28 2012-07-28 17:03:06Z trzntaat $
 * $Revision: 28 $
 */

#ifndef T2TYPES_H_INCLUDED
#define T2TYPES_H_INCLUDED


namespace turska
{
    // Primitive types should be defined in Turs2config.h:
    // floats: F32,F64
    // integers: I8,UI8,I16,UI16,I32,UI32
    // char: C8,C16
    // real number: TREAL (can be either fixed or floating point)
    
    enum GRAPHICS_ENUM
    {
        //! Use current default flags when loading images. \sa Graphics
        G_USE_DEFAULT_FLAGS = 0xffffffff
    };

    struct PixelRectangle;

    struct Rectangle
    {
        Rectangle(TREAL x = 0, TREAL y = 0, TREAL width = 0, TREAL height = 0) :
            x(x), y(y), width(width), height(height) {}
        inline Rectangle(const PixelRectangle &pr);
        TREAL x, y;
        TREAL width, height;
        bool isInside(TREAL x, TREAL y) const
        {
            const Rectangle &r = *this;
            return x >= r.x && y >= r.y && x < r.x + r.width && y < r.y + r.height;
        }
    };

    //! Defines a rectangle inside pixel image data.
    struct PixelRectangle
    {
        PixelRectangle(I16 x = 0, I16 y = 0, I16 width = 0, I16 height = 0) :
            x(x), y(y), width(width), height(height) {}
        inline PixelRectangle(const Rectangle &r) :
            x((I16)r.x), y((I16)r.y), width((I16)r.width), height((I16)r.height) {}
        I16 x, y;
        I16 width, height;
    };

    inline Rectangle::Rectangle(const PixelRectangle &pr) :
        x((TREAL)pr.x), y((TREAL)pr.y), width((TREAL)pr.width), height((TREAL)pr.height)
    {
    }

    //! Specifies a color as unsigned 8-bit integer values.
    struct Color
    {
        union {
            UI8 c[4];
            struct {
                UI8 r;
                UI8 g;
                UI8 b;
                UI8 a;
            };
        };

        Color() { set(0xffffffff); }
        Color(UI8 red, UI8 green, UI8 blue, UI8 alpha = 255) { set(red, green, blue, alpha); }
        Color(TREAL red, TREAL green, TREAL blue, TREAL alpha = (TREAL)1.0f) { set(red, green, blue, alpha); }
        Color(I32 color) { set(color); }
        Color(UI32 color) { set(color); }
        
        inline void set(UI8 red, UI8 green, UI8 blue, UI8 alpha = 255)
        {
            r = red;
            g = green;
            b = blue;
            a = alpha;
        }
        
        inline void set(TREAL red, TREAL green, TREAL blue, TREAL alpha = (TREAL)1.0f)
        {
            r = (UI8)(red * 255);
            g = (UI8)(green * 255);
            b = (UI8)(blue * 255);
            a = (UI8)(alpha * 255);
        }
        
        inline void set(UI32 color)
        {
            a = (color >> 24) & 0xff;
            r = (color >> 16) & 0xff;
            g = (color >> 8) & 0xff;
            b = color & 0xff;
        }

        inline void set(I32 color) { set((UI32)color); }

        inline UI32 asUI32() { return (a << 24) | (r << 16) | (g << 8) | b; }
        inline I32 asI32() { return (I32)asUI32(); }
    };

    enum KEY_ACTION
    {
        KA_NONE = 0,
        KA_CHAR,     // character entered
        KA_NEXT,     // typically tab
        KA_PREVIOUS, // typically shift-tab
        KA_LEFT,     // move left
        KA_RIGHT,    // move right
        KA_UP,       // move up
        KA_DOWN,     // move down
        KA_SELECT    // enter, kp-enter
    };
    
    struct KeyEvent
    {
        KEY_ACTION action;
        UI32 character; //!< Pressed key as unicode character.
        bool shift;
        bool control;
        bool alt;
        //! Contains current platform's event structure for more details.
        /*! For SDL this is a pointer to a SDL_keysym struct.
         * For Irrlicht this is a pointer to a SKeyInput struct.
         */
        const void *platformEvent;
    };
    
} // namespace turska


#endif // !T2TYPES_H_INCLUDED
